class Node:
    def __init__(self, data=None):
        self.data = data
        self.left = None
        self.right = None


class BTree:
    def __init__(self, dataset):
        self.root = self.create_node(dataset)

    def create_node(self, dataset, i=0):
        if i >= len(dataset):
            return

        node = Node()
        node.data = dataset[i]

        left_index = 2 * (i + 1)
        right_index = left_index + 1

        node.left = self.create_node(dataset, left_index - 1)
        node.right = self.create_node(dataset, right_index - 1)

        return node

    def __loop(self, node, flag=-1):
        if flag < 0:
            # 先序
            yield node.data
        if node.left:
            for d in self.__loop(node.left):
                yield d
        if flag == 0:
            # 中序
            yield node.data
        if node.right:
            for d in self.__loop(node.right):
                yield d
        if flag > 0:
            # 后序
            yield node.data

    def __iter__(self):
        return self.__loop(self.root)


#
# class BTreeIterator:
#     def __init__(self, root_node):
#         self.node_ptr = root_node
#
#     def __next__(self):

tree = BTree((1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
for d in tree:
    print(d)
